<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\AdminGoodsAddRequest;
class GoodsController extends Controller
{
    //  商品管理控制器
	//获取商品列表
    public function getIndex(Request $request)
    {
    	//获取数据
    	// dd($request->all());
		$num = $request->input('num',10);
		$likes = $request->input('likes');
		//判断是否有查询条件语句
		if($likes){
			$goods = DB::table('shop_goods')->where('gname','like','%'.$likes.'%')->paginate($num);
		}else{
			$goods = DB::table('shop_goods')->paginate($num);
		}
        foreach ($goods as $k => $v) {
            //设置字段xname
            $goods[$k]->xname = DB::table('shop_type')->where('tid',$v->tid)->value('tname');
        }
		// 获取参数列表
		$list = $request->all();
    	// dd($goods);
        $arr = [0=>'新品',1=>'上架',2=>'下架']; 
    	//解析模板
    	return view('admin.goods.index',['goods'=>$goods,'num'=>$num,'likes'=>$likes,'list'=>$list,'arr'=>$arr]);
    }
    //获取商品添加页
    public function getAdd()
    {
    	
    	//获取分类的数据
    	$res = DB::select("select *,concat(path,',',tid) as paths from shop_type order by paths");
    	//设置分类的格式
    	$cates = CateController::getCates($res);
    	//解析模板
    	return view('admin.goods.add',['cates'=>$cates]);
    }
    //商品添加数据处理
    public function postInsert(AdminGoodsAddRequest $request)
    {
    	// dd($request->all());
    	$data = $request->except(['_token','gpic','editorValue']);
        //商品描述
    	$data['gdesc'] = $request->input('editorValue');
        //商品折扣价格
        $data['zkprice'] = $data['dcont']*$data['price']/10;
        //尺寸处理
        $data['gsize'] = implode('@@',$data['gsize']).'@@';
      	$data['ctime'] = time();
        //没有图片上传
        if(empty($request->gpic)){
            return back()->with('error','没有上传图片，请重新添加商品');
        }
    	$data['gpic'] = $this->postUpload($request,'gpic');
    	//操作数据库
    	// dd($data);
    	$gid = DB::table('shop_goods')->insertGetId($data);
        // dd($gid);
        $res = DB::table('shop_description')->insert(['gid'=>$gid]);
    	if($res){
    		return redirect('/admin/goods/index')->with('success','商品添加成功');
    	}else{
    		return back()->with('error','商品添加失败');
    	}
    }
    //上传图片路径获取
    private function postUpload(Request $request,$face)
   	{
	   	//检测是否有文件上传
	   	if($request->hasFile($face)){
	   		//获取文件后缀名
	   		$ext = $request->file($face)->getClientOriginalExtension();
	   		//随机文件名
	   		$name = md5(time().rand(1,99999));
	   		$request->file($face)->move('./uploads/goods/',$name.'.'.$ext);
	   		return '/uploads/goods/'.$name.'.'.$ext;
	   	}
   	}
    public function postDel(Request $request){
        //获取id
        $id = $request->input('id');
        // dd($id);
        //获取图片路径
        $paths = DB::table('shop_goods')->where('gid', $id)->value('gpic');
        // dd($paths);
        //删除
        $res = DB::table('shop_goods')->where('gid', $id)->delete();
        //判断是否存在   删除图片
        if($paths){
            unlink('.'.$paths);
        }
        echo $res;
    }
    //商品修改操作
    //显示修改页面
    public function getEdit(Request $request)
    {
        //获取分类的数据
        $res = DB::select("select *,concat(path,',',tid) as paths from shop_type order by paths");
        //设置分类的格式
        $cates = CateController::getCates($res);
        //获取要修改用户的数据
        $id = $request->input('id');
        $goods = DB::table('shop_goods')->where('gid', $id)->first();
        // dd($goods);
        //解析html
        return view('admin.goods.edit',['goods'=>$goods,'cates'=>$cates]);
    }
    //商品修改数据处理
    public function postUpdate(AdminGoodsAddRequest $request)
    {
        $paths = null;
        // dd($request->all());
        $data = $request->except('_token','editorValue');
        //判断图片是否上传
        if($request->hasFile('gpic')){
            $data['gpic'] = $this->postUpload($request,'gpic');
            //如果修改图片  就删除原来的图片
            //获取图片路径
            $paths = DB::table('shop_goods')->where('gid',$request->input('gid'))->value('gpic');
        }
        //商品折扣价格
        $data['zkprice'] = $data['dcont']*$data['price']/10;
        //尺寸处理
        $data['gsize'] = implode('@@',$data['gsize']).'@@';
        $data['gdesc'] = $request->input('editorValue');
        // dd($data);
        $res = DB::table('shop_goods')->where('gid', $request->input('gid'))->update($data);
        if($res){
            //成功
            //判断是否存在   删除图片
            if($paths){
                unlink('.'.$paths);
            }
            return redirect('/admin/goods/index')->with('success','商品修改成功');
        }else{
            //失败
            return back()->with('error','商品修改失败');
        }
    }

    //商品上架和下架方法
    public function postPutaway(Request $request)
    {
        //获取id
        $id = $request->input('id');
        //获取商品状态
        $status = DB::table('shop_goods')->where('gid', $id)->value('status');
        if( $status == 0 || $status == 2 ){
            $res = DB::table('shop_goods')->where('gid', $id)->update(['status'=>1]);
        }else{
            $res = DB::table('shop_goods')->where('gid', $id)->update(['status'=>2]);
        }
        echo $res;
    }

    //商品详情中的图片添加
    public function postImgs(Request $request)
    {
        $id = $request->all();
        var_dump($id);
    }
    //获取商品详情数据
    public function postShuju(Request $request){
        // 获取id

        $id = $request->input('id');
        //查询商品数据
        $goods = DB::table('shop_goods')
            ->join('shop_description', 'shop_goods.gid', '=', 'shop_description.gid')
            ->select('shop_goods.*', 'shop_description.*')
            ->get();
        //获取到该商品的数据
        foreach($goods as $k=>$v){
            if($v->gid == $id){
                $good[0] = $v; 
            }
        }
        //图片地址数据
        $mpic = DB::table('shop_img')->where('gid',$id)->get();
        //加入到发送数据中
        $good[0]->mpic = $mpic;
        $res = json_encode($good[0]);
        echo $res;
    }
    //图片上传
    public function postAddimg(Request $request){
        //上传数量限制
        // dd($request->all());
        // dd($request->file('files'));
        if(count($request->file('files'))>=4){
            return back()->with('error','上传图片过多');
        }
        //没有上传图片
        if($request->file('files')[0]==null){
            return back()->with('error','未选中图片,请添加图片');
        }
        //没有选择颜色
        if(empty($request->file('files'))){
            return back()->with('error','没有选择颜色');
        }
        //判断颜色图片数量是否一致
        if(count($request->input('color')) !=count($request->file('files'))){
            return back()->with('error','图片数量与选择颜色个数不一致');
        }
        foreach($request->file('files') as $k=>$v){
            $data[$k]['gid'] = $request->input('gid');
            //检测是否有文件上传
            if($request->hasFile('files')){
                //获取文件后缀名
                $ext = $v->getClientOriginalExtension();
                //随机文件名
                $name = md5(time().rand(1,99999));
                $v->move('./uploads/goods/',$name.'.'.$ext);
                $data[$k]['mpic'] = '/uploads/goods/'.$name.'.'.$ext;
                $data[$k]['color'] = $request->input('color')[$k];
            }
        }
        // dd($data);
        $res = DB::table('shop_img')->insert($data);
        if($res){
            //成功
            return redirect('/admin/goods/index')->with('success','图片添加成功');
        }else{
            //失败
            return back()->with('error','图片添加失败');
        }
    }
    // 删除图片
    public function postDelimg(Request $request)
    {
        // dd($request->all());
        //获取图片地址
        $mpic = $request->input('src');
        $res = DB::table('shop_img')->where('mpic',$mpic)->delete();
            //判断是否存在   删除图片
            if($res){
                unlink('.'.$mpic);
            }
        echo $res;
    }
    //商品详情修改
    public function postUpdades(Request $request)
    {
        //商品数据
        $data = $request->only('gname','gbrand');
        //商品详情数据
        $des = $request->only('gsex','gage','gaddr','gseason');
        //修改至商品中
        
        $res1 = DB::table('shop_goods')->where('gid', $request->input('gid'))->update($data);
        echo $res1;
        //修改至商品详情中
        $res2 = DB::table('shop_description')->where('gid', $request->input('gid'))->update($des);
        echo $res2;
        
        
        
        
    }
}  
    
    